<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input clearable @clear="clearInput" placeholder="请输入内容" v-model="queryData.query">
            <el-button slot="append" icon="el-icon-search" @click="queryGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods" class="addBtn">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" width="100px" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" width="100px" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="100px" >
          <template slot-scope="scope">
            {{scope.row.add_time | formateDate('yyyy-MM-dd hh:mm')}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              @click="removeGoods(scope.row.goods_id)"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageCurrentChange"
        :current-page="queryData.pagenum"
        :total="total"
        :page-sizes="[5,10,15,20]"
        :page-size="queryData.pagesize"
        layout="total,sizes,prev,pager,next,jumper"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 商品信息列表
      goodsList: null,
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0
    }
  },
  created() {
    this.queryGoodsList()
  },
  methods: {
    // 每页数量条数
    pageSizeChange(size) {
      this.queryData.pagesize = size
      this.queryGoodsList()
    },
    // 当前页码发生改变
    pageCurrentChange(current) {
      this.queryData.pagenum = current
      this.queryGoodsList()
    },
    // 清空输入框
    clearInput() {
      this.queryData.query = ''
      this.queryGoodsList()
    },
    // 移出商品
    removeGoods(id) {
      this.$confirm('删除商品', '此操作将永久删除该商品数据', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`)
          if (res.meta.status !== 200) {
            return this.$message.error('删除失败')
          }
          this.$message.success('删除成功')
          this.queryGoodsList()
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 添加商品
    addGoods() {
      this.$router.push('/goods/add')
    },
    // 获取商品列表
    async queryGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>
<style lang="less" scoped>
.addBtn {
  margin-left: 15px;
}
</style>
